package com.pda.gongpin.ui.home;

import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;

import com.pda.gongpin.R;
import com.pda.gongpin.adapter.MyViewPagerAdapter;
import com.pda.gongpin.dialog.AffirmDialog;
import com.pda.gongpin.ui.about.AboutFragment;
import com.pda.gongpin.view.NoSlidingViewPager;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.HOMEACTIVITY)
public class HomeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_user)
    RadioButton rbUser;
    @BindView(R.id.rg_home)
    RadioGroup rgHome;
    @BindView(R.id.vp_content)
    NoSlidingViewPager vpContent;
    private List<Fragment> fragments=new ArrayList<>();
    private MyViewPagerAdapter adapter;
    @Override
    public int getView() {
        return R.layout.activity_home;
    }

    @Override
    public void initView() {
        HomeFragment homeFragment=new HomeFragment();
        AboutFragment mineFragment=new AboutFragment();
        fragments.add(homeFragment);
        fragments.add(mineFragment);
        adapter=new MyViewPagerAdapter(getSupportFragmentManager(),fragments);
        vpContent.setAdapter(adapter);
        vpContent.setCurrentItem(0);
        vpContent.setOffscreenPageLimit(1);
        rgHome.setOnCheckedChangeListener(this);
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }
    private long exitTime=0;

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event){
        if(keyCode== KeyEvent.KEYCODE_BACK){
            exit();
            return false;
        }
        return super.onKeyDown(keyCode,event);
    }

    private void exit(){
        if((System.currentTimeMillis()-exitTime)>2000) {
            Toast.makeText(getApplicationContext(),
                    "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else{
                finish();
                System.exit(0);
            }
    }

    int currentFragment=0;
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_home){
            currentFragment=0;
        }else if (checkedId == R.id.rb_user){
            currentFragment=1;
        }
        vpContent.setCurrentItem(currentFragment);
    }

}